package com.ruoyi.weiriya.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.weiriya.domain.*;
import com.ruoyi.weiriya.mapper.WNonVipSettingMapper;
import com.ruoyi.weiriya.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.weiriya.mapper.WVipSettingMapper;
import com.ruoyi.weiriya.domain.WVipSetting;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 会员购买设置Service业务层处理
 * 
 * @author lynn
 * @date 2023-08-01
 */
@Service
public class WVipSettingServiceImpl extends ServiceImpl<WVipSettingMapper, WVipSetting> implements IWVipSettingService
{

    @Autowired
    private WVipSettingMapper vipSettingMapper;

    @Autowired
    private IWCompanyService companyService;

    @Override
    public List<WVipSetting> getList(WVipSetting wVipSetting) {
        if(StringUtils.isNotNull(wVipSetting.getSettingType()) && wVipSetting.getSettingType() == 1) {
            return vipSettingMapper.getChargeList(wVipSetting.queryWrapper());
        } else if(StringUtils.isNotNull(wVipSetting.getSettingType()) && wVipSetting.getSettingType() == 2) {
            return vipSettingMapper.getDepositList(wVipSetting.queryWrapper());
        } else {
            List<WVipSetting> chargeList = vipSettingMapper.getChargeList(wVipSetting.queryWrapper());
            List<WVipSetting> depositList = vipSettingMapper.getDepositList(wVipSetting.queryWrapper());
            chargeList.addAll(depositList);
            return chargeList;
        }
    }

    @Override
    public boolean saveWVipSetting(WVipSetting wVipSetting) {

        if(!checkWVipSetting(wVipSetting)){
            throw new ServiceException("金额范围为：0.01 - 9999.99");
        }

        Integer settingType = wVipSetting.getSettingType();
        String vehicleType = wVipSetting.getVehicleType();
        if(StringUtils.isNull(settingType) || settingType > 2 || settingType < 1){
            throw new ServiceException("请选择收费类型！");
        }

        if(StringUtils.isBlank(vehicleType)){
            throw new ServiceException("请选择车辆类型！");
        }

        if(StringUtils.isBlank(wVipSetting.getAreaId())){
            throw new ServiceException("请选择点位！");

        }else {
            List<WArea> areaListByCompanyId = companyService.getAreaListByCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
            List<String> collect = areaListByCompanyId.stream().map(WArea::getId).collect(Collectors.toList());
            if(!collect.contains(wVipSetting.getAreaId())){
                throw new ServiceException("权限不足！");
            }
        }

        WVipSetting vipSetting = this.getOne(new LambdaQueryWrapper<WVipSetting>()
                .eq(WVipSetting::getAreaId, wVipSetting.getAreaId())
                .eq(WVipSetting::getSettingType, settingType)
                .eq(WVipSetting::getVehicleType, vehicleType));
        if(StringUtils.isNotNull(vipSetting)){
            throw new ServiceException("该区域的此类型收费标准已存在！");
        }

        return this.save(wVipSetting);
    }

    private boolean checkWVipSetting(WVipSetting wVipSetting) {
        Double monthAmount = wVipSetting.getMonthAmount();
        Double quarterAmount = wVipSetting.getQuarterAmount();
        Double yearAmount = wVipSetting.getYearAmount();

        if(StringUtils.isNull(monthAmount) || StringUtils.isNull(quarterAmount) || StringUtils.isNull(yearAmount)){
            throw new ServiceException("金额不能为空！");
        }
        if(monthAmount < 0.01 || monthAmount > 9999.99){
            return false;
        }
        if(quarterAmount < 0.01 || quarterAmount > 9999.99){
            return false;
        }
        if(yearAmount < 0.01 || yearAmount > 9999.99){
            return false;
        }
        return true;
    }

    @Override
    public boolean updateWVipSettingById(WVipSetting wVipSetting) {

        if(!checkWVipSetting(wVipSetting)){
            throw new ServiceException("金额范围为：0.01 - 9999.99");
        }
        return this.updateById(wVipSetting);
    }

    @Override
    public WVipSetting getWVipSettingById(String id, Integer settingType) {
        if(1 == settingType){
            return vipSettingMapper.getChargeWVipSettingById(id);
        } else {
            return vipSettingMapper.getDepositWVipSettingById(id);
        }
    }

}
